Why does gcc warn about incompatible struct assignment with a `self = [super initDesignatedInit];' c

Posted by gavinbeatty on Stack Overflow See other posts from Stack Overflow or by gavinbeatty
Published on 2010-03-12T14:52:33Z Indexed on 2010/03/12 22:57 UTC
Read the original article Hit count: 116

Filed under:
|
|

I have the following base/derived class setup in Objective-C:

@interface ASCIICodeBase : NSObject {
 @protected
  char code_[4];
}
- (Base *)initWithASCIICode:(const char *)code;
@end

@implementation ASCIICodeBase
- (ASCIICodeBase *)initWithCode:(const char *)code len:(size_t)len {
  if (len == 0 || len > 3) {
    return nil;
  }
  if (self = [super init]) {
    memset(code_, 0, 4);
    strncpy(code_, code, 3);
  }
  return self;
}
@end

@interface CountryCode : ASCIICodeBase
- (CountryCode *)initWithCode:(const char *)code;
@end
@implementation CountryCode
- (CountryCode *)initWithCode:(const char *)code {
  size_t len = strlen(code);
  if (len != 2) {
    return nil;
  }
  self = [super initWithCode:code len:len]; // here
  return self;
}
@end

On the line marked "here", I get the following gcc warning:

warning: incompatible Objective-C types assigning 'struct ASCIICodeBase *', expected 'struct CurrencyCode *'

Is there something wrong with this code or should I have the ASCIICodeBase return id? Or maybe use a cast on the "here" line?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about gcc